package org.comet.auxiliary.client.swing;

import org.comet.auxiliary.common.util.MapBean;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * @author wq
 * @date 2019-11-12
 * @description full-compensate-client
 */
public class OperationCellEditor extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
    private ActionListener listener;
    private Color bgColor;

    public OperationCellEditor(ActionListener listener, Color bgColor) {
        this.bgColor = bgColor;
        this.listener = listener;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return initPanel(value);
    }

    private JPanel initPanel(Object value) {
        String seq = "0";
        String status = "";
        if (value instanceof MapBean) {
            MapBean bean = (MapBean) value;
            seq = bean.getString("phone");
            status = bean.getString("status");
        }
        JPanel panel = new JPanel();
        panel.setBackground(Color.white);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        JButton login = new JButton("登录");
        JButton logout = new JButton("退出");
        login.addActionListener(listener);
        logout.addActionListener(listener);

        login.setActionCommand("login-" + seq);
        logout.setActionCommand("logout-" + seq);

        if ("在线".equals(status)) {
            panel.add(logout);
        } else {
            panel.add(login);
        }
        panel.setBackground(bgColor);
        return panel;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        return initPanel(value);
    }

    @Override
    public Object getCellEditorValue() {
        return "999";
    }
}
